using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class EditorWindow_GenAssetBundle : EditorWindow
{
     [MenuItem("BDFrameWork工具箱/2.AssetBundle打包", false, 0)]
            public static void Open()
            {
                var window = EditorWindow.GetWindow<EditorWindow_GenAssetBundle>(false, "AB打包工具");
                window.Show();
            }
            
            public void OnGUI(){
                
                TipsGUI();
                LastestGUI();
            }

            void LastestGUI()
            {
                if (GUILayout.Button("一键打包[美术资源]", GUILayout.Width(380), GUILayout.Height(30)))
                {
                    //开始打包
                    BuildAssetBundle(ab输出路径);
                }
            }


            private void OnEnable()
            {
                ab输出路径 = PlayerPrefs.GetString("AssetBundle输出路径", "");
                ALL_SHADER_VARAINT_RUNTIME_PATH = PlayerPrefs.GetString("ALL_SHADER_VARAINT_RUNTIME_PATH", "Assets/Resource/Runtime/Shader/");
            }

            private string ab输出路径;
            /// <summary>
            /// 提示UI
            /// </summary>
            void TipsGUI()
            {
                GUILayout.Label("2.资源打包", EditorGUIHelper.TitleStyle);
                GUILayout.Space(5);
                GUILayout.Label("资源根目录:");
                GUILayout.Label(ab输出路径);
                // 选择文件夹按钮
                if (GUILayout.Button("选择输出文件夹"))
                {
                    string selectedPath = EditorUtility.OpenFolderPanel("选择AssetBundle输出文件夹", Application.dataPath, "");
                    if (!string.IsNullOrEmpty(selectedPath))
                    {
                        // 转换为相对路径（相对于项目根目录）
                        if (selectedPath.StartsWith(Application.dataPath))
                        {
                            ab输出路径 = "Assets" + selectedPath.Substring(Application.dataPath.Length);
                        }
                        else
                        {
                            ab输出路径 = selectedPath;
                        }
                        PlayerPrefs.SetString("AssetBundle输出路径", ab输出路径);
                    }
                }
                
                EditorGUILayout.LabelField("shadervariants文件路径:", ALL_SHADER_VARAINT_RUNTIME_PATH);
                // 选择文件夹按钮
                if (GUILayout.Button("修改shadervariants文件路径"))
                {
                    string selectedPath = EditorUtility.OpenFolderPanel("修改shadervariants文件路径", Application.dataPath, "");
                    if (!string.IsNullOrEmpty(selectedPath))
                    {
                        // 转换为相对路径（相对于项目根目录）
                        if (selectedPath.StartsWith(Application.dataPath))
                        {
                            ALL_SHADER_VARAINT_RUNTIME_PATH = "Assets" + selectedPath.Substring(Application.dataPath.Length)+"/";
                        }
                        else
                        {
                            ALL_SHADER_VARAINT_RUNTIME_PATH = selectedPath+"/";
                        }
                        PlayerPrefs.SetString("ALL_SHADER_VARAINT_RUNTIME_PATH", ALL_SHADER_VARAINT_RUNTIME_PATH);
                    }
                }
            }
            
            
            
            
            
            
            
            
            
            
            string ALL_SHADER_VARAINT_RUNTIME_PATH = "Assets/Resource/Runtime/Shader/";
            private string ALL_SHADER = "AllShaders.shadervariants";

            /// <summary>
            /// Shadervariant资源地址
            /// </summary>
            
            
            /// <summary>
            /// 打包资源
            /// </summary>
            public void BuildAssetBundle(string outputPath)
            {
                //搜集keyword
                ShaderCollection.SimpleGenShaderVariant(ALL_SHADER_VARAINT_RUNTIME_PATH+ALL_SHADER);
                //打包
                RuntimePlatform platform = RuntimePlatform.Android;

                BDFrameWorkEditorSetting setting = BDFrameWorkEditorSetting.Load();

                var assetConfig = setting.BuildAssetBundle;
                //生成Assetbundlebunle
                AssetBundleEditorToolsV2.GenAssetBundle(outputPath, platform);
                AssetDatabase.Refresh();
                Debug.Log("资源打包完毕");
            }

}
